package com.example.lin.api;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import sun.misc.BASE64Encoder;

import javax.net.ssl.SSLContext;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: EmsV1
 * @description:
 * @author: Lin.Gu
 * @date: 2025/2/21
 */
public class EmsV1 {

    public static void main(String[] args) throws Exception {
        // 创建一个信任所有证书的SSLContext
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
        SSLContext sslContext = SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy)
                .build();

        // 创建忽略主机名验证的SSLConnectionSocketFactory
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);

        // 创建HttpClient
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        // 使用HttpClient创建RequestFactory
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);

        // 创建RestTemplate并设置RequestFactory
        RestTemplate restTemplate = new RestTemplate(requestFactory);


        String apikey = "key123xydJDPT";
        String logistics_interface = "<BatchGetWaybillNo><CreatedTime>2025-02-20 13:48:15</CreatedTime><CustomerNo>90000001885440</CustomerNo><eventSource>XYD</eventSource><MailType>6</MailType><count>1</count></BatchGetWaybillNo>";

        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        String ret = base64en.encode(md5.digest((logistics_interface + apikey).getBytes(StandardCharsets.UTF_8)));
        System.out.println(ret);

        String url = "https://211.156.197.233/iwaybillno-web/a/iwaybillBatch/receive";

        // 定义query参数
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("logistics_interface", logistics_interface);
        queryParams.put("data_digest", ret);
        queryParams.put("msg_type", "BatchGetWaybillNo");
        queryParams.put("ecCompanyId", "XYD");

        // 构建带有query参数的URL
        URI uri = UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("logistics_interface", queryParams.get("logistics_interface"))
                .queryParam("data_digest", queryParams.get("data_digest"))
                .queryParam("msg_type", queryParams.get("msg_type"))
                .queryParam("ecCompanyId", queryParams.get("ecCompanyId"))
                .build()
                .toUri();

        System.out.println(uri);

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 创建HttpEntity对象（包含请求体和请求头）
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);

        // 发送POST请求
        ResponseEntity<String> response = restTemplate.exchange(
                uri,
                HttpMethod.POST,
                requestEntity,
                String.class
        );

        // 输出响应结果
        System.out.println("Response Status: " + response.getStatusCode());
        System.out.println("Response Body: " + response.getBody());

    }
}
